/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/22
 * Time: 09:31
 */
$(function(){
    //上传学历证明
    $('.upload-education-profile').ajaxUpload({
        url: '/v1/upload',
        name:'education-profile',
        top: '190px',
        onSubmit:function (data){
            $('#text-education-profile').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-education-profile>img').attr('src','/pictures/' + info.savename);
                $('.input-education-profile').val(info.savename);
                $('#text-education-profile').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //上传学位证明
    $('.upload-degree-profile').ajaxUpload({
        url: '/v1/upload',
        name:'degree-profile',
        top: '190px',
        onSubmit:function (data){
            $('#text-degree-profile').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-degree-profile>img').attr('src','/pictures/' + info.savename);
                $('.input-degree-profile').val(info.savename);
                $('#text-degree-profile').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //上传职称证书
    $('.upload-professional-title-profile').ajaxUpload({
        url: '/v1/upload',
        name:'professional-title-profile',
        top: '190px',
        onSubmit:function (data){
            $('#text-professional-title-profile').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-professional-title-profile>img').attr('src','/pictures/' + info.savename);
                $('.input-professional-title-profile').val(info.savename);
                $('#text-professional-title-profile').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //上传婚姻证明
    $('.upload-marriage-profile').ajaxUpload({
        url: '/v1/upload',
        name:'marriage-profile',
        top: '190px',
        onSubmit:function (data){
            $('#text-marriage-profile').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-marriage-profile>img').attr('src','/pictures/' + info.savename);
                $('.input-marriage-profile').val(info.savename);
                $('#text-marriage-profile').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //上传房产证明
    $('.upload-house-profile').ajaxUpload({
        url: '/v1/upload',
        name:'house-profile',
        top: '190px',
        onSubmit:function (data){
            $('#text-house-profile').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-house-profile>img').attr('src','/pictures/' + info.savename);
                $('.input-house-profile').val(info.savename);
                $('#text-house-profile').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //-----------------------------------------------------------

    var option_manage = '<option value="2">二级</option>';
    option_manage += '<option value="4">四级</option>';
    option_manage += '<option value="5">五级</option>';
    option_manage += '<option value="6">六级</option>';
    option_manage += '<option value="7">七级</option>';
    option_manage += '<option value="8">八级</option>';
    option_manage += '<option value="9">九级</option>';
    option_manage += '<option value="10">十级</option>';
    option_manage += '<option value="11">十一级</option>';
    option_manage += '<option value="12">十二级</option>';

    var option_skill = '<option value="3">三级</option>';
    option_skill += '<option value="4">四级</option>';
    option_skill += '<option value="5">五级</option>';
    option_skill += '<option value="6">六级</option>';
    option_skill += '<option value="7">七级</option>';
    option_skill += '<option value="8">八级</option>';
    option_skill += '<option value="9">九级</option>';
    option_skill += '<option value="10">十级</option>';
    option_skill += '<option value="11">十一级</option>';
    option_skill += '<option value="12">十二级</option>';

    sendAjax('/v1/teacher/units.json', 'get', {}, function(json){
        var option = '';
        $.each(json.body.rows, function(n, v) {
            option += '<option value="'+ v.id+'">'+ v.name+'</option>';

        });
        $('.file-unit').append(option);
        sendAjax('/v1/teacher/file.json', 'get', {}, function(json){
            var teacher = json.body.data;
            $('.file-number').text(teacher.number);
            $('.file-realname').val(teacher.realname);
            var gender = teacher.gender == 1 ? '男' : '女';
            if (teacher.gender == 1) {
                $('input[name="file-gender"]:first').prop('checked', 'checked');
            } else {
                $('input[name="file-gender"]:last').prop('checked', 'checked');
            }
            $('.file-identity').val(teacher.identity);
            $('.file-mobile').val(teacher.mobile);
            $('.file-email').val(teacher.email);
            $('.file-unit').val(teacher.unit);
            $('.file-unit option').each(function(){
                if ($(this).val() == teacher.unit) {
                    $(this).attr('selected', 'selected');
                }
            });
            $('.file-join-at').val(teacher.join_at);
            $('.file-work-age').val(teacher.work_age);
            $('.file-education').val(teacher.education_code);
            $('.file-education option').each(function(){
                if ($(this).val() == teacher.education_code) {
                    $(this).attr('selected', 'selected');
                }
            });
            var education_profile = teacher.education_profile == '' ? '/images/profile.png' : '/pictures/'+teacher.education_profile;
            $('.input-education-profile').val(teacher.education_profile);
            $('.img-education-profile').attr('src', education_profile);
            $('.file-degree').val(teacher.degree_code);
            $('.file-degree option').each(function(){
                if ($(this).val() == teacher.degree_code) {
                    $(this).attr('selected', 'selected');
                }
            });
            var degree_profile = teacher.degree_profile == '' ? '/images/profile.png' : '/pictures/'+teacher.degree_profile;
            $('.input-degree-profile').val(teacher.degree_profile);
            $('.img-degree-profile').attr('src', degree_profile);
            var job_type = teacher.job_type == 0 ? 1 : teacher.job_type;
            $('.file-job-type').val(job_type);
            $('.file-job-type option').each(function(){
                if ($(this).val() == job_type) {
                    $(this).attr('selected', 'selected');
                }
            });
            if (teacher.job_type == 2) {
                $('.file-job-code').html('');
                $('.file-job-code').append(option_skill);
            } else {
                $('.file-job-code').html('');
                $('.file-job-code').append(option_manage);
            }
            var job_code = teacher.job_code == 0 ? (job_type == 2 ? 3 : 2) : teacher.job_code;
            $('.file-job-code').val(job_code);
            $('.file-job-code option').each(function(){
                if ($(this).val() == job_code) {
                    $(this).attr('selected', 'selected');
                }
            });
            var professional_title_code = teacher.professional_title_code == 0 ? 4 : teacher.professional_title_code;
            $('input[name="file-professional-title-code"]').each(function(){
                if ($(this).val() == professional_title_code) {
                    $(this).prop('checked', 'checked');
                }
            });
            $('.file-professional-check').val(teacher.professional_check);
            var professional_title_profile = teacher.professional_title_profile == '' ? '/images/profile.png' : '/pictures/'+teacher.professional_title_profile;
            $('.input-professional-title-profile').val(teacher.professional_title_profile);
            $('.img-professional-title-profile').attr('src', professional_title_profile);

            $('.file-duty-on').val(teacher.duty_on);
            $('.file-duty-type').val(teacher.duty_type);
            $('.file-capacity').val(teacher.capacity);

            if (teacher.marriage < 2) {
                $('.marriage-profile-form').addClass('hidden');
                $('.has-spouse-group').addClass('hidden');
                // 相对位移
                $('input[name="house-profile"]').css('top', '1285px');
            } else {
                $('.marriage-profile-form').removeClass('hidden');
                $('input[name="marriage-profile"]').css('width', '128px');
                $('input[name="file-marriage"]:last').prop('checked', 'checked');
                $('.marriage-title').text('已婚证明');
                $('.has-spouse-group').removeClass('hidden');
                $('input[name="house-profile"]').css('top', '1497px');
            }
            var marriage_profile = teacher.marriage_profile == '' ? '/images/profile.png' : '/pictures/'+teacher.marriage_profile;
            $('.input-marriage-profile').val(teacher.marriage_profile);
            $('.img-marriage-profile').attr('src', marriage_profile);
            $('.file-spouse-name').val(teacher.spouse_name);
            $('.file-spouse-unit').val(teacher.spouse_unit);
            if (teacher.has_house == 1) {
                $('input[name="file-has-house"]:first').prop('checked', 'checked');
                $('.house-title').text('房产证明');
            } else {
                $('input[name="file-has-house"]:last').prop('checked', 'checked');
                $('.house-title').text('无房证明');
            }
            var house_profile = teacher.house_profile == '' ? '/images/profile.png' : '/pictures/'+teacher.house_profile;
            $('.input-house-profile').val(teacher.house_profile);
            $('.img-house-profile').attr('src', house_profile);
            $('.file-remark').val(teacher.remark);
        });
    });

    //改变职务类型
    $('.file-job-type').on('change', function(){
        if ($(this).val() == 2) {
            $('.file-job-code').val(3);
            $('.file-job-code').html('');
            $('.file-job-code').append(option_skill);
        } else {
            $('.file-job-code').val(2);
            $('.file-job-code').html('');
            $('.file-job-code').append(option_manage);
        }
    });

    //改变婚否
    $('input[name="file-marriage"]').on('change', function(){
        if ($(this).val() == 1) {
            $('.marriage-profile-form').addClass('hidden');
            $('.has-spouse-group').addClass('hidden');
            $('input[name="house-profile"]').css('top', '1285px');
        } else {
            $('.marriage-profile-form').removeClass('hidden');
            $('input[name="marriage-profile"]').css('width', '128px');
            $('.marriage-title').text('已婚证明');
            $('.has-spouse-group').removeClass('hidden');
            $('input[name="house-profile"]').css('top', '1497px');
        }
    });

    //改变房产情况
    $('input[name="file-has-house"]').on('change', function(){
        if ($(this).val() == 1) {
            $('.house-title').text('房产证明');
        } else {
            $('.house-title').text('无房证明');
        }
    });



    //保存信息
    $('.btn-save').on('click', function(){
        var data = {
            realname: $('.file-realname').val(),
            gender: $('input[name="file-gender"]:checked').val(),
            identity: $('.file-identity').val(),
            mobile: $('.file-mobile').val(),
            email: $('.file-email').val(),
            unit: $('.file-unit').val(),
            join_at: $('.file-join-at').val()+'-01 00:00:01',
            work_age: $('.file-work-age').val(),
            education_code: $('.file-education').val(),
            education_profile: $('.input-education-profile').val(),
            degree_profile: $('.input-degree-profile').val(),
            job_type: $('.file-job-type').val(),
            job_code: $('.file-job-code').val(),
            professional_title_code: $('input[name="file-professional-title-code"]:checked').val(),
            professional_check: $('.file-professional-check').val(),
            professional_title_profile: $('.input-professional-title-profile').val(),
            duty_on: $('.file-duty-on').val(),
            duty_type: $('.file-duty-type').val(),
            capacity: $('.file-capacity').val(),
            marriage: $('input[name="file-marriage"]:checked').val(),
            marriage_profile: $('.input-marriage-profile').val(),
            has_house: $('input[name="file-has-house"]:checked').val(),
            house_profile: $('.input-house-profile').val(),
            remark: $('.file-remark').val()
        };
        if (data.marriage >= 2) {
            data.spouse_name = $('.file-spouse-name').val();
            data.spouse_unit = $('.file-spouse-unit').val();
        }
        if (checkForm(data)) {
            sendAjax('/v1/teacher/save/file', 'post', {data:data}, function(json){
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        location.href = '/teacher/file.html';
    });

});

function checkForm(data) {
    return true;
}